package com.cntcdt.api.db.dao.base;

import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.ResultSet;
import com.coola.jutil.sql.DBEngine;
import com.coola.jutil.sql.DBFactory;
import com.coola.jutil.data.DataPage;
import com.coola.jutil.sql.ResultPage;
import  javax.sql.rowset.CachedRowSet;
import com.cntcdt.api.db.model.ShoppingCommentModel;

/**
 * 这是cdt_shopping_comment的Dao实现类的基类
 */
public abstract class ShoppingCommentDaoBase {

    /**
     * 日志处理类实例
     */
    private static Log logger = LogFactory.getLog(ShoppingCommentDaoBase.class);
    /**
     * 只读DB引擎
     */
    private DBEngine readDBEngine = DBFactory.getDBEngine("main-statistic-b");
    /**
     * 只写DB引擎
     */
    private DBEngine writeDBEngine = DBFactory.getDBEngine("main-statistic");
    /**
     * cdt_shopping_comment表
     */
    private static final String TABLE = "cdt_shopping_comment";

    /**
     * 默认构造方法
     */
    public ShoppingCommentDaoBase() {
    }

    /**
     * 插入一条数据
     * @param  model ShoppingCommentModel对象
     * @return 返回插入影响行数
     */
    public int insert(ShoppingCommentModel model) {
        DBEngine localEngine = DBFactory.getKeepConnDBEngine("main-statistic");
        String sql = "INSERT INTO " + TABLE + " (Content,ShopImg,UserImg,Position,ShopName,AddTime,UpdateTime) VALUES (?,?,?,?,?,?,?)";
        Object[]  paramObj = {model.getContent(),model.getShopImg(),model.getUserImg(),model.getPosition(),model.getShopName(),model.getAddTime(),model.getUpdateTime()};
        int id=0;
        try {
             id = localEngine.executeUpdate(sql, paramObj);
             ResultSet rs = localEngine.executeQuery("select last_insert_id() as id");
             if (rs.next()) {
               id = rs.getInt("id");
             }
        } catch(Exception e) {
           e.printStackTrace();
        } finally {
           localEngine.close();
        }
        return id;
    }

    /**
     * 修改一条数据
     * @param  model ShoppingCommentModel对象
     * @return 修改成功返回true,否则返回false
     */
    public boolean update(ShoppingCommentModel model) {
        String sql = "UPDATE  " + TABLE + " SET Content = ? ,ShopImg = ? ,UserImg = ? ,Position = ? ,ShopName = ? ,AddTime = ? ,UpdateTime = ?  WHERE Id = ? ";
        Object[]  paramObj = {model.getContent(),model.getShopImg(),model.getUserImg(),model.getPosition(),model.getShopName(),model.getAddTime(),model.getUpdateTime(),model.getId()};
        try {
             return writeDBEngine.executeUpdate(sql, paramObj)>0;
        } catch(Exception e) {
           e.printStackTrace();
        }
        return false;
     }

    /**
     * 按主键删除一条数据
     * @param id id
     * @return 删除成功返回true,否则返回false
     */
    public boolean deleteByPrimaryKey(Integer id) {
        String sql = "DELETE FROM " + TABLE + " WHERE id = ?";
       Object[] paramObjs = {id};
       try{
           return writeDBEngine.executeUpdate(sql, paramObjs)>0; 
       } catch (Exception e) {
            e.printStackTrace();
       }
       return false;
 }

    /**
     * 获得model集合
     * @param  sql  查询语句
     * @return 返回Model集合
     */
    protected List<ShoppingCommentModel> queryModelList(String sql) {
        List<ShoppingCommentModel > modelList = new ArrayList<ShoppingCommentModel>();
        try {
             ResultSet rs = readDBEngine.executeQuery(sql);
             while( rs.next()) {
                  modelList.add(new ShoppingCommentModel(rs));
             }
        } catch(Exception e) {
           e.printStackTrace();
        }
        return modelList;
     }
    
    /**
     * 获得model集合
     * @param  sql   查询语句
     * @param  objs  参数集
     * @return 返回Model集合
     */
    protected List<ShoppingCommentModel> queryModelList(String sql, Object[] paramObjs) {
        List<ShoppingCommentModel > modelList = new ArrayList<ShoppingCommentModel>();
        try {
             ResultSet rs = readDBEngine.executeQuery(sql, paramObjs);
             while( rs.next()) {
                  modelList.add(new ShoppingCommentModel(rs));
             }
        } catch(Exception e) {
           e.printStackTrace();
        }
        return modelList;
     }

    /**
     * 按主键查询model
     * @param id id
     * @return model
     */
    public ShoppingCommentModel findByPrimaryKey(Integer id) {
        String sql = "SELECT * FROM " + TABLE + " WHERE id = ?";
       Object[] paramObjs = {id};
       List<ShoppingCommentModel > modelList = this.queryModelList(sql, paramObjs);
       if(modelList != null && modelList.size() > 0){ 
             return  modelList.get(0);
       } 
       return null;
   }

    /**
     * 获得model集合
     * @return 返回Model集合
     */
    public List<ShoppingCommentModel> findAll() {
        String sql = "SELECT * FROM " + TABLE ;
        return queryModelList(sql);
    }

    /**
     * 获分页
     * @param sql 查询条件
     * @param pageSize 分页大小
     * @param pageNo 页码
     * @return 返回分页对象
     */
    protected DataPage<ShoppingCommentModel> findPage(String sql, int pageSize, int pageNo) {
        CachedRowSet rs = null; 
        DataPage<ShoppingCommentModel> resultList = null;
        try{
           ResultPage page = readDBEngine.queryPage(sql,pageSize, pageNo);
           rs = page.getRecord();
           List<ShoppingCommentModel> list = new ArrayList<ShoppingCommentModel>();
            while( rs.next()) {
               list.add(new ShoppingCommentModel(rs));
            }
           if (list.size() > 0) {
              resultList = new DataPage<ShoppingCommentModel>(list, page.getTotalRecordCount(),pageSize, pageNo);
           }
        } catch(Exception e) {
             e.printStackTrace();
        }
        return resultList;
    }

    /**
     * 获分页
     * @param pageSize 分页大小
     * @param pageNo 页码
     * @return 返回分页对象
     */
    public DataPage<ShoppingCommentModel> findPage(int pageSize, int pageNo) {
        String sql = "SELECT * FROM " + TABLE ; 
        return findPage(sql, pageSize, pageNo);
    }
}
